package com.vehicle.mq.event;

import com.lmax.disruptor.EventHandler;
import com.vehicle.mq.constant.EventCommandEnum;
import com.vehicle.mq.handler.MessageDispatcher;
import com.vehicle.mq.handler.MessageHandler;
import com.vehicle.mq.message.BaseMessage;
import com.vehicle.mq.message.MessageEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by yantingbin on 2017/11/30.
 */
public class MessageEventHandler implements EventHandler<MessageEvent> {

    private static Logger LOGGER = LoggerFactory.getLogger(MessageEventHandler.class);

    private final MessageDispatcher MD = MessageDispatcher.getInstance();

    @Override
    public void onEvent(MessageEvent messageEvent, long l, boolean b) throws Exception {
        LOGGER.info("current message is {}",messageEvent.getBaseMessage());

        MessageHandler messageHandler = MD.getMessageHandler(EventCommandEnum.toCMD(messageEvent.getBaseMessage().getCommand()));

        messageHandler.handle(messageEvent.getBaseMessage());
    }
}
